﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace MASSBAckUp
{

    public partial class FolderSelection : Form
    {
        private String RootFolder;

        public String SelectedFolder
        {
            get { return RootFolder; }
        }

        
        private ImageList _smallImageList = new ImageList();
        
        public FolderSelection(String RootFolder)
        {
            this.RootFolder = RootFolder;
            InitializeComponent();
            TreeNode nd = FoldersTree.Nodes.Add(@"\");
            UpdateTree(RootFolder, nd.Nodes);
            nd.Expand()
                ;
        }

        public void UpdateTree(String Folder, TreeNodeCollection node)
        {
            try
            {
                
                //System.Drawing.Icon.ExtractAssociatedIcon

                IEnumerable<String> dirs = Directory.EnumerateDirectories(Folder);
                foreach (String DirName in Enumerable.OrderBy<String, String>(dirs, delegate(String a) { return a; }))
                {

                    TreeNode nd = node.Add(System.IO.Path.GetFileName(DirName));
                    UpdateTree(System.IO.Path.Combine(Folder, DirName), nd.Nodes);
                }
            }
            catch (Exception)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void FoldersTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            FolderName.Text = e.Node.FullPath;
            if (FolderName.Text.Length >=2 && FolderName.Text.Substring(0, 2) == @"\\")
            {
                FolderName.Text = FolderName.Text.Substring(1, FolderName.Text.Length - 1);
            }
            RootFolder = FolderName.Text;
        }

    }
}
